package com.yztc.orangeentertainment.ui.main.subscribefragment.contentitem;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.SubCategoryAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticle;
import com.yztc.orangeentertainment.bean.starconbean.CategoryDataBean;
import com.yztc.orangeentertainment.bean.starconbean.CategoryListBean;
import com.yztc.orangeentertainment.config.ChannalUrlConfig;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.main.channalarticle.ChannalArticleActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/10.
 * 订阅内容item
 */
public class CategoryActivity extends BaseActivity implements CategoryContract.View {


    @InjectView(R.id.iv_cate)
    ImageView ivCate;
    @InjectView(R.id.sdv_img)
    SimpleDraweeView sdvImg;
    @InjectView(R.id.tv_cate_name)
    TextView tvCateName;
    @InjectView(R.id.tv_cate_sub)
    TextView tvCateSub;
    @InjectView(R.id.rel_cate)
    RelativeLayout relCate;
    @InjectView(R.id.frame_cate)
    FrameLayout frameCate;
    @InjectView(R.id.tv_title_cate)
    TextView tvTitleCate;
    @InjectView(R.id.toolBar_cate)
    Toolbar toolBarCate;
    @InjectView(R.id.collapsing_cate)
    CollapsingToolbarLayout collapsingCate;
    @InjectView(R.id.appbar_cate)
    AppBarLayout appbarCate;
    @InjectView(R.id.rv_category)
    RecyclerView mRecyclerView;
    private String id;
    private int page=1;
    private CategoryPresenter presenter;
    private int lastVisibleItem;
    private LinearLayoutManager linearLayoutManager;
    private SubCategoryAdapter adapter ;
    private List<CategoryListBean> totalList=new ArrayList<>();
    private boolean flag;
    @Override
    public int getLayoutId() {
        return R.layout.activity_categoty;
    }

    /**
     * 获取内容list集合
     * @param categoryDataBean
     */
    @Override
    public void getCategoryListSuccess(final CategoryDataBean categoryDataBean) {
        Glide.with(this).load(categoryDataBean.getInfo().getImg()).into(ivCate);
        ivCate.setColorFilter(0x50000000);
        sdvImg.setImageURI(categoryDataBean.getInfo().getImg());
        tvCateName.setText(categoryDataBean.getInfo().getName());
        appbarCate.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    //展开状态
                    relCate.setVisibility(View.VISIBLE);
                    tvTitleCate.setVisibility(View.GONE);
                } else if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                    //折叠状态
                    relCate.setVisibility(View.GONE);
                    tvTitleCate.setVisibility(View.VISIBLE);
                    tvTitleCate.setText(categoryDataBean.getInfo().getName());
                } else {
                    //中间状态
                    relCate.setVisibility(View.GONE);
                    tvTitleCate.setVisibility(View.VISIBLE);
                    tvTitleCate.setText(categoryDataBean.getInfo().getName());
                }
            }
        });
        //分页加载
        if (categoryDataBean.getList() != null) {
            totalList.addAll(categoryDataBean.getList());

            adapter.setList(totalList);
            if (page==1) {
                mRecyclerView.setAdapter(adapter);
            }else {
                adapter.notifyDataSetChanged();
            }
            //点击事件
            adapter.setListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent=new Intent(CategoryActivity.this, ChannalArticleActivity.class);
                    intent.putExtra("id",totalList.get(position).getId()+"");
                    startActivity(intent);
                }

                @Override
                public void onItemLongClick(View view, int position) {

                }
            });
        }
    }

    @Override
    public void getCategoryListFail(String msg) {

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        initView();
        initPresenter();
    }

    /**
     * 初始化内容对应的present
     */
    private void initPresenter() {
        presenter = new CategoryPresenter(this);
        Map<String, String> map = new HashMap<>();
        //传参
        map.put(ChannalUrlConfig.Params.ID, id);
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getCategoryFromNet(map);
    }

    /**
     * 初始化视图
     */
    private void initView() {
        adapter = new SubCategoryAdapter(this );
        id = getIntent().getStringExtra("id");
        //recyclerview加载
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(10));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) == adapter
                        .getItemCount()) {
                    page++;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            initPresenter();
                            adapter.notifyDataSetChanged();
                        }
                    }, 2000);
                }
            }
        });
        //订阅按钮的点击
        tvCateSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!flag){
                    flag=true;
                    tvCateSub.setText(R.string.sub);
                }else {
                    flag=false;
                    tvCateSub.setText(R.string.subed);
                }
            }
        });
    }
}